package Runde2;

import java.util.HashSet;

public abstract class Activity {
	private Person supervisor;
	private int lva;
	private HashSet<String> participants;
	private int maxParticipants;
	private String ownerPw;
	
	/**
	 * Creates a new activity.
	 * 
	 * @param supervisor - activity's supervisor
	 * @param lva - nr of associated lva
	 * @param maxParticipants - maximum amount of participants
	 * @param pw - associated lva-owner's password
	 * @throws InsufficientPermissionsException - thrown of wrong associated lva-owner's password is given
	 */
	public Activity (Person supervisor, int lva, int maxParticipants, String pw) throws InsufficientPermissionsException {
		if (!LVAManager.getLVA(lva).getOwner().validate(pw)) {
			throw new InsufficientPermissionsException("Wrong password (lva-owner)");
		}
		this.lva = lva;
		this.maxParticipants = maxParticipants;
		this.supervisor = supervisor;
		this.participants = new HashSet<String>();
		this.ownerPw = pw;
	}
	
	/**
	 * Adds a participant to the activity.
	 * 
	 * @param pw The student's password.
	 * @param matrNr The student's unique number.
	 * @throws InsufficientPermissionsException If the student is not registered with the course of this exam or has entered a wrong password.
	 */
	public void addParticipant(String pw, String matrNr) throws InsufficientPermissionsException {
		if (!LVAManager.getLVA(lva).existsStudent(matrNr)) {
			throw new InsufficientPermissionsException("You are not registered with the course this exam belongs to.");
		} 
		
		try {
			StudentManager.getStud(matrNr).authorizeForGrading(this, pw);
			participants.add(matrNr); 
		} catch (InsufficientPermissionsException e) {
			throw new InsufficientPermissionsException(e.getMessage());
		}
		
	}
	
	public void setGrade(String matrNr, int grade, String pw) throws InsufficientPermissionsException, InvalidGradeException {
		if (!supervisor.validate(pw)) {
			throw new InsufficientPermissionsException("Wrong password");
		}
		if(!participants.contains(matrNr)) {
            throw new InsufficientPermissionsException("This student is not participating at this exam.");
		}

		if (grade < 1 || grade > 5) {
			throw new InvalidGradeException("Invalid grade given (" + grade + ")");
		}
		
		try {
			StudentManager.getStud(matrNr).setGrade(this, grade);
			
			if (StudentManager.getStud(matrNr).getTries(lva) >= 3){
				LVAManager.getLVA(lva).kick(matrNr, ownerPw);
			}
			
		} catch (InsufficientPermissionsException e) {
			throw new InsufficientPermissionsException(e.getMessage());
		}
	}
	
	/**
	 * Removes a member from the activity
	 * 
	 * @param pw - The supervisor's / student's password.
	 * @param matrNr - The # of the student to remove.
	 * @throws InsufficientPermissionsException - Thrown when a wrong password is entered.
	 */
	public void removeParticipant(String pw, String matrNr) throws InsufficientPermissionsException {
		if (supervisor.validate(pw) || StudentManager.getStud(matrNr).validate(pw)) {
			participants.remove(matrNr);
		} else {
			throw new InsufficientPermissionsException("You are not allowed to do that.");
		}
	}
	
	/**
	 * Getter for maxParticipants.
	 * 
	 * @return The maximum number of participants.
	 */
	public int getMaxParticipants() {
		return maxParticipants;
	}
	
	/**
	 * Getter for associated LVA
	 * 
	 * @return The maximum number of participants.
	 */
	public int getLVA() {
		return lva;
	}
	
	/**
	 * Makes a list of all participants only the supervisor can view. 
	 * 
	 * @param pw The examiner's password.
	 * @return A readable list of all participants.
	 * @throws InsufficientPermissionsException Thrown if the password is wrong.
	 */
	public String toString(String pw) throws InsufficientPermissionsException {
		if (supervisor.validate(pw)) {
			String output = "";
			for (String s: participants) {
				output += "#" + s + " " + StudentManager.getStud(s).toString()+ "\n";
			}
			return output;
		} else {
			throw new InsufficientPermissionsException("You are not allowed to do that.");
		}
	}
}
